/**
 * Created by Spring on 2017/4/7.
 */
({
  loadAccount:function(cmp) {
    console.log('-------into helper');
    var action = cmp.get("c.getAccounts");
    action.setParams({"acc":cmp.get('v.acc')});
    action.setCallback(this, function (response) {
      var state = response.getState();
      console.log(state);
      if (cmp.isValid() && state === "SUCCESS") {
        cmp.set("v.accountList", response.getReturnValue());
        console.log(response.getReturnValue());
        var spinner = cmp.find("mySpinner");
        $A.util.toggleClass(spinner, "slds-hide");
      }
    })
    $A.enqueueAction(action);
  },

  delAccount:function(cmp, delIds, newAccList){
   var spinner = cmp.find("mySpinner");
   $A.util.toggleClass(spinner, "slds-hide");
    var action = cmp.get("c.delAccountByIds");
    action.setParam("ids",delIds);
    action.setCallback(this, function (response) {
      var toastEvent = $A.get("e.force:showToast");
      var state = response.getState();
      console.log(state);
      if (cmp.isValid() && state === "SUCCESS") {
        console.log('dada');
        cmp.set("v.accountList", newAccList);
        toastEvent.setParams({
          "title": "Success!",
          "message": "删除成功!",
          "type" : "success"
        });

        //this.rfDashBoard(cmp);
      }
      if (cmp.isValid() && state === "ERROR") {
        console.log(response.getError()[0].message);
        toastEvent.setParams({
          "title": "error!",
          "message": "保存失败,查看console.log!",
          "type": "error"
        });
      }
      $A.util.toggleClass(spinner, "slds-hide");
      toastEvent.fire();
    })
    $A.enqueueAction(action);
  },

  saveAccount:function(cmp){
    var spinner = cmp.find("mySpinner");
    $A.util.toggleClass(spinner, "slds-hide");
    var accList = cmp.get("v.accountList");
    console.log(accList);
    var accListJson = JSON.stringify(accList);
    var action = cmp.get("c.saveAccountList");
    action.setParam("accountInners",accListJson);
    action.setCallback(this, function (response) {
      var toastEvent = $A.get("e.force:showToast");
      var state = response.getState();
      if (cmp.isValid() && state === "SUCCESS") {
        toastEvent.setParams({
          "title": "Success!",
          "message": "保存成功!",
          "type":"success"
        });

        //这里为什么需要这么做,父级的变量才会变,是由于缓冲么???
        var newAccList = [];
        for(var i = 0 ;i<accList.length;i++){
          newAccList.push(accList[i]);
        }
        cmp.set("v.accountList", newAccList);

        //通知父组件发出通知,数据有修改,仪表盘需要重新刷新
        this.rfDashBoard(cmp);
      }
      if (cmp.isValid() && state === "ERROR") {
        console.log(response.getError()[0].message);
        toastEvent.setParams({
          "title": "Success!",
          "message": "保存失败,查看console.log!",
          "type":"error"
        });
      }
      $A.util.toggleClass(spinner, "slds-hide");
      toastEvent.fire();
    })
    $A.enqueueAction(action);
  },

  rfDashBoard:function(cmp){
    //CMP 事件通过组件获得
    var evt = cmp.getEvent('rfDashBoardEvt');
    evt.setParams({'flag':true});
    evt.fire();
  }
})